"""
https://www.tensorflow.org/guide/autodiff
"""

import sys
from python_ai.common.xcommon import *
import tensorflow as tf

print(tf.__version__)
tf.random.set_seed(777)

sep('y = x**2')
x = tf.Variable(4.0, name='x')
a = tf.constant(3.0, name='a')
with tf.GradientTape() as tape1:
    with tf.GradientTape() as tape2:
        with tf.GradientTape() as tape3:
            y = x ** a
        dy_dx = tape3.gradient(y, x)
    dy_dx2 = tape2.gradient(dy_dx, x)
dy_dx3 = tape1.gradient(dy_dx2, x)
sep('dydx')
print(dy_dx)
sep('dydx2')
print(dy_dx2)
sep('dydx3')
print(dy_dx3)
